package com.zrrd.yunchmall.product.controller;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.product.entity.ProductAttribute;
import com.zrrd.yunchmall.product.entity.ProductAttributeCategory;
import com.zrrd.yunchmall.product.entity.ProductAttributeValue;
import com.zrrd.yunchmall.product.service.IProductAttributeCategoryService;
import com.zrrd.yunchmall.product.service.IProductAttributeService;
import com.zrrd.yunchmall.product.service.IProductAttributeValueService;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 产品属性分类表 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/productAttribute/category")
@Api(tags = "商品服务-商品属性类别")
@SuppressWarnings("all")
public class ProductAttributeCategoryController {

    @Autowired
    private IProductAttributeCategoryService attributeCategoryService;
    @Autowired
    private IProductAttributeValueService valueService;
    @Autowired
    private IProductAttributeService attributeService;

    @ApiOperation("查询列表")
    @GetMapping("/list")
    public ResponseResult list(int pageNum, int pageSize, String keyword) {
        QueryWrapper queryWrapper = null;
        if (!StringUtil.isEmpty(keyword)) {
            queryWrapper = new QueryWrapper();
            queryWrapper.like("name",keyword);
        }
        return new ResponseResult(200,"查询成功",
                attributeCategoryService.page(new Page<>(pageNum,pageSize), queryWrapper));
    }
    @ApiOperation("创建")
    @PostMapping("/create")
    public ResponseResult create(String name) {
        ProductAttributeCategory productAttributeCategory = new ProductAttributeCategory();
        productAttributeCategory.setName(name);
        attributeCategoryService.save(productAttributeCategory);
        return new ResponseResult(200, "创建成功");
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    public ResponseResult delete(@PathVariable("id") long id) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        attributeCategoryService.remove(updateWrapper);
        return new ResponseResult(200, "删除成功");
    }

    @ApiOperation("更改")
    @GetMapping("/update/{id}")
    public ResponseResult update(@PathVariable("id") long id,
                                 @RequestBody ProductAttributeCategory productAttributeCategory) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        attributeCategoryService.update(productAttributeCategory, updateWrapper);
        return new ResponseResult(200, "更改成功");
    }
    @ApiOperation("筛选属性")
    @GetMapping("/list/withAttr")
    public ResponseResult listWithAttr() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List<ProductAttributeCategory> listVale = attributeCategoryService.list(queryWrapper);
        for (int i = 0; i < listVale.size(); i++) {
            Long id = listVale.get(i).getId();
            queryWrapper.clear();
            queryWrapper.eq("product_attribute_category_id", id);
            List<ProductAttribute> list = attributeService.list(queryWrapper);
            listVale.get(i).setProductAttributeList(list);
        }
        return new ResponseResult(200, "查询成功", listVale);
    }

}
